package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelComment;
import com.ktgj.customer.service.ITravelCommentService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 评论Controller
 * 
 * @author ruoyi
 * @date 2021-11-12
 */
@ControllerAdvice
@RestController
@RequestMapping("/travelComment")
public class TravelCommentController extends BaseController
{
    @Autowired
    private ITravelCommentService travelCommentService;

    /**
     * 查询评论列表
     */
    //@PreAuthorize(hasPermi = "customer:comment:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelComment travelComment)
    {
        startPage();
        List<TravelComment> list = travelCommentService.selectTravelCommentList(travelComment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    //@PreAuthorize(hasPermi = "customer:comment:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelComment travelComment) throws IOException
    {
        List<TravelComment> list = travelCommentService.selectTravelCommentList(travelComment);
        ExcelUtil<TravelComment> util = new ExcelUtil<TravelComment>(TravelComment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    //@PreAuthorize(hasPermi = "customer:comment:query")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return AjaxResult.success(travelCommentService.selectTravelCommentById(commentId));
    }

    /**
     * 新增评论
     */
    //@PreAuthorize(hasPermi = "customer:comment:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelComment travelComment)
    {
        return toAjax(travelCommentService.insertTravelComment(travelComment));
    }

    /**
     * 修改评论
     */
    //@PreAuthorize(hasPermi = "customer:comment:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelComment travelComment)
    {
        return toAjax(travelCommentService.updateTravelComment(travelComment));
    }

    /**
     * 删除评论
     */
    //@PreAuthorize(hasPermi = "customer:comment:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(travelCommentService.deleteTravelCommentByIds(commentIds));
    }

    @GetMapping("/lists")
    public TableDataInfo lists()
    {
        startPage();
        List<TravelComment> list = travelCommentService.queryTravelCommentList();
        return getDataTable(list);
    }

    /**
     * 添加评论
     * @param request
     * @param travelComment
     * @return
     */
    @PostMapping("saveComment")
    public AjaxResult saveComment(HttpServletRequest request, TravelComment travelComment) {
        return travelCommentService.saveComment(request, travelComment);
    }

    /**
     * 根据旅游团id查询相应评论
     */
    @PostMapping("/queryGroupCommentById")
    public AjaxResult queryGroupCommentById(@RequestBody Map<String, Object> map) {
        Long groupId = Long.valueOf(map.get("groupId").toString());
        return AjaxResult.success(travelCommentService.queryGroupCommentById(groupId));
    }

    /**
     * 查询指定旅游产品评论信息
     * */
    @PostMapping("/commentVoList")
    public AjaxResult commentVoList(@RequestBody Map<String, Object> map) {
        if (map.get("groupId") != null && !map.get("groupId").equals("")){
            Long groupId = Long.valueOf(map.get("groupId").toString());
            List<CommentVo> commentVoList;
            try {
                commentVoList = travelCommentService.commentVoList(groupId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (commentVoList != null && commentVoList.size() > 0){
                return AjaxResult.success(commentVoList);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
